///注入css
const kittlent_style = document.createElement("style");
kittlent_style.type = "text/css";
kittlent_style.innerHTML = `
        body {
            margin: 0;
            padding: 0;
            overflow: auto;
            overflow-y: hidden;
        }
`;
async function kittlenMinShow(url) {
    if ('documentPictureInPicture' in window) {
        // console.log("🚀 浏览器支持 PiP 功能！");
    } else {
        console.warn("⚠️ 当前浏览器版本或该页面不支持 PiP 功能");
        return;
    }
    let defWidth = localStorage.getItem('kittlent_width') || 200;
    let defHeight = localStorage.getItem('kittlent_height') || 300;
    const kittlent_body = `
    <div>
        <iframe style="height: 100vh; width: 100%;" height="500px" width="100%"
            src="${url}"
            frameborder="0"></iframe>
    </div>
    `
    const rootDiv = document.createElement('div');

    const pipWindow = await window.documentPictureInPicture.requestWindow({
        width: defWidth,
        height: defHeight,
        ///是否再窗口右上角显示返回标签页按钮。默认为 false.
        disallowReturnToOpener: false,
    });
    rootDiv.innerHTML = kittlent_body;
    pipWindow.document.body.appendChild(rootDiv);
    pipWindow.document.head.appendChild(kittlent_style)
    pipWindow.addEventListener("pagehide", (event) => {
        console.log("已退出 PIP 窗口");
    });

    pipWindow.addEventListener("resize", (event, b) => {
        _updatePipWindowDefSize(pipWindow.outerWidth, pipWindow.outerHeight)
        // console.log(pipWindow.);
    });

    pipWindow.addEventListener('focus', () => {
        console.log("PiP 窗口进入了焦点状态");
    });

    pipWindow.addEventListener('blur', () => {
        console.log("PiP 窗口失去了焦点");
    });
}

let _updatePWDSSetTimer = null;

function _updatePipWindowDefSize(width, height) {
    clearTimeout(_updatePWDSSetTimer);
    _updatePWDSSetTimer = setTimeout(() => {
        localStorage.setItem('kittlent_width', width);
        localStorage.setItem('kittlent_height', height);
    }, 500);

}